import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { AfMessageService, AfNavigationService } from '@core';
import { STColumn, STColumnTag, STColumnBadge, STComponent } from '@delon/abc/st';
import { ModalHelper, _HttpClient } from '@delon/theme';

import { CodeGenService } from '../../codegen.service';

@Component({
  selector: 'app-meta-workspace',
  templateUrl: './workspace.component.html'
})
export class MetaWorkspaceComponent implements OnInit {
  rows: any[] = [];

  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    // { title: 'id', index: 'id' },
    { title: '工作空间名称', index: 'name' },
    { title: '显示名', index: 'label' },
    {
      title: '状态',
      type: 'tag',
      index: 'status',
      tag: {
        normal: { text: '正常', color: 'success' },
        distable: { text: '禁用', color: 'orange' }
      }
    },
    { title: '更新人', index: 'lastModifiedBy' },
    { title: '更新日期', type: 'date', index: 'lastModifiedDate' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          icon: 'edit',
          tooltip: `修改账号信息`,
          click: record => {
            this.editNavigate(record, 0);
          }
        },
        {
          text: '权限',
          icon: 'safety',
          tooltip: `分配账号信息`,
          click: record => {
            this.editNavigate(record, 1);
          }
        }
      ]
    }
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private cdr: ChangeDetectorRef,
    private msgSrv: AfMessageService,
    private navSrv: AfNavigationService,
    private cgSrv: CodeGenService
  ) {}

  ngOnInit(): void {
    this.query();
  }

  addNavigate(): void {
    this.navSrv.navigateByUrl('/codegen/meta/workspace/edit?btn=add');
  }

  editNavigate(record: any, tagIndex: any) {
    this.navSrv.navigateByUrl(`/codegen/meta/workspace/edit?btn=edit&record=${JSON.stringify(record)}&index=${tagIndex}`);
  }

  query(event: any = undefined) {
    this.cgSrv.getWorkspace().subscribe(res => {
      if (res.status) {
        this.rows = res.data;
      }
    });
  }
}
